/* global window */
import modelExtend from 'dva-model-extend'
import { sum, excel } from 'services/companyAccountsStatistics'
import { message } from 'antd'
import { pageModel } from './common'
import * as companyAccount from 'services/adminCompanyAccount.js'
import * as companyChargeType from 'services/companyChargeType.js'
import * as basicData from 'services/basicData.js'
import queryString from 'query-string'

export default modelExtend(pageModel, {
  namespace: 'companyAccountsStatistics',

  state: {
    companyAccount: [],
    companyChargeType: [],
    currenciesCodeData: [],
  },

  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/companyaccountsstatistics') {
          // if (location.search) {
          const payload = queryString.parse(location.search)
          dispatch({
            type: 'query',
            payload,
          })
          // }
          dispatch({ type: 'getCompanyAccount' })
          dispatch({ type: 'getChargeType' })
          dispatch({ type: 'getCurrencyCode' })
        }
      })
    },
  },
  effects: {
    * query ({ payload = {} }, { call, put }) {
      const data = yield call(sum, payload)
      const listData = data.data
      if (data) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: listData.data,
            pagination: {
              current: Number(payload.page) || 1,
              pageSize: Number(payload.pageSize) || 10,
              total: listData.total,
            },
          },
        })
      }
    },

    * getCompanyAccount ({ payload = {} }, { call, put }) {
      const companyAccountRes = yield call(companyAccount.listall)
      let companyAccountList = []
      companyAccountRes.data.forEach(((value) => {
        companyAccountList.push({ companyAccountId: value.id, name: value.name })
      }))
      if (companyAccountRes.success) {
        yield put({
          type: 'updateState',
          payload: {
            companyAccount: companyAccountList,
          },
        })
      }
    },

    * getChargeType ({ payload = {} }, { call, put }) {
      const companyChargeTypeRes = yield call(companyChargeType.list)
      let companyChargeTypeList = []
      companyChargeTypeRes.data.data.forEach(((value) => {
        companyChargeTypeList.push({ key: value.id, value: value.typeName })
      }))
      if (companyChargeTypeRes.success) {
        yield put({
          type: 'updateState',
          payload: {
            companyChargeType: companyChargeTypeList,
          },
        })
      }
    },

    * getCurrencyCode ({ payload = {} }, { call, put }) {
      const currenciesData = yield call(basicData.listall, { type: 'Currencies' })
      if (currenciesData.success) {
        yield put({
          type: 'updateState',
          payload: {
            currenciesCodeData: currenciesData.data,
          },
        })
      }
    },
    * excel ({ payload }, { call, put }) {
      payload = {
        id: 1,
        ...payload,
      }
      const data = yield call(add, payload)
      if (data.success) {
        message.success('create success')
        yield put({ type: 'hideModal' })
        yield put({ type: 'list' })
      } else {
        message.error('create failed')
        throw data
      }
    },

  },

  reducers: {

  },
})
